package com.eagle.epay.config;

import com.eagle.epay.constant.RedisConstant;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.ClusterServersConfig;
import org.redisson.config.Config;
import org.redisson.config.SentinelServersConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * redisson配置
 */
@Configuration
@Slf4j
public class RedissonManager {

	@Autowired
	RedisPropertiesConfig redisPropertiesConfig;

	@Bean
	@ConditionalOnProperty(prefix = "redis", name = "mode", havingValue = RedisConstant.SENTINEL, matchIfMissing = true)
	public RedissonClient getSentinelRedisson(){
		log.info("init SENTINEL redission..");
		Config config = new Config();
		String[] nodesTemp = this.redisPropertiesConfig.getSentinelAddresses().split(",");
		List<String> newNodes = new ArrayList<>(nodesTemp.length);
		Arrays.stream(nodesTemp).forEach((index) -> newNodes.add(
				index.startsWith("redis://") ? index : "redis://" + index));
		SentinelServersConfig serverConfig = config.useSentinelServers()
				.addSentinelAddress(newNodes.toArray(new String[0]))
				.setMasterName(this.redisPropertiesConfig.getMasterName());
		if(StringUtils.isNotBlank(this.redisPropertiesConfig.getPassword())) {
			serverConfig.setPassword(this.redisPropertiesConfig.getPassword());
		}
		return Redisson.create(config);
	}

	@Bean
	@ConditionalOnProperty(prefix = "redis", name = "mode", havingValue = RedisConstant.CLUSTER, matchIfMissing = true)
	public RedissonClient getClusterRedisson(){
		log.info("init CLUSTER redission..");
		Config config = new Config();
		String[] nodesTemp = this.redisPropertiesConfig.getClusterAddresses().split(",");
		List<String> newNodes = new ArrayList<>(nodesTemp.length);
		Arrays.stream(nodesTemp).forEach((index) -> newNodes.add(
				index.startsWith("redis://") ? index : "redis://" + index));
		ClusterServersConfig serverConfig = config.useClusterServers().addNodeAddress(newNodes.toArray(new String[0]));
		if(StringUtils.isNotBlank(this.redisPropertiesConfig.getPassword())) {
			serverConfig.setPassword(this.redisPropertiesConfig.getPassword());
		}
		return Redisson.create(config);
	}

	@Bean
	@ConditionalOnProperty(prefix = "redis", name = "mode", havingValue = RedisConstant.STANDALONE, matchIfMissing = true)
	public Redisson getLocalRedission(){
		log.info("init STANDALONE redission..");
		Config config = new Config();
		StringBuffer sb = new StringBuffer();
		sb.append("redis://").append(this.redisPropertiesConfig.getHostName())
				.append(":").append(this.redisPropertiesConfig.getPort());
		log.info("local addr: {}", sb.toString());
		config.useSingleServer().setAddress(sb.toString());
		Redisson redisson = (Redisson) Redisson.create(config);
		return redisson;
	}

}
